package in.smartbillpay.web.controllers;

import in.smartbillpay.web.models.Authority;
import in.smartbillpay.web.models.SBPUser;
import in.smartbillpay.web.services.UserService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/login")
public class SBPLoginController {

	private static final Logger logger = LoggerFactory
			.getLogger(SBPLoginController.class);

	@Autowired
	private UserService userService;

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Render the Login form
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView getLoginForm() {
		logger.info("Inside getLoginOForm()");		
		
		ModelAndView mav = new ModelAndView("login");
		
		return  mav;		
	}
	
	/**
	 * Render the Login form
	 */
	@RequestMapping(value = "/invalid_session", method = RequestMethod.GET)
	public ModelAndView getSessionInvalidForm() {
		logger.info("Inside getSessionInvalidForm()");		
		
		ModelAndView mav = new ModelAndView("invalid_session");
		
		return  mav;		
	}
	/**
	 * Route the logged in user to the appropriate home screen based on his/her
	 * authority (role)
	 */
	@RequestMapping(value = "/route_user", method = RequestMethod.GET)
	public String routeUser() {
		logger.info("Inside routeUser()");
		// if no match found, redirect user to the home screen
		String redirectView = "redirect:../";
		Object principal = new Object() ;
		SecurityContext securityContext = SecurityContextHolder.getContext();
   		 if(securityContext != null) {
   			 Authentication authentication = securityContext.getAuthentication();
   			 if(authentication != null) {
   				 String username = "";
   				 principal = authentication.getPrincipal();
   				if (principal != null && principal instanceof UserDetails) {
                    username = ((UserDetails)principal).getUsername();
                    logger.info("userNAme is -" + username);
                   } 
				 if(principal != null && principal instanceof SBPUser){
					 username = ((SBPUser)principal).getFirstName() + " " + ((SBPUser)principal).getLastName();
					 logger.info("userNAme is -" + username);
					 
				 }
   			 } 
   				 
   			 }
		if (principal instanceof SBPUser) {
			List<Authority> authorityList = (List<Authority>) ((SBPUser) principal)
					.getAuthorityList();
			logger.info("Authority count is -" + authorityList.size());
			int priority = 0;
			// iterate through the authority list and identify the highest
			// priority
			for (Authority authority : authorityList) {
				if (authority.getName().equalsIgnoreCase("ROLE_ADMIN")) {
					priority = 1;
				} else if (authority.getName().equalsIgnoreCase(
						"ROLE_BRANCH_MANGER")) {
					priority = 2;
				} else if (authority.getName().equalsIgnoreCase("ROLE_AGENT")) {
					priority = 3;
					// assume the no priority for agent
				}

				switch (priority) {
				case 1:
					redirectView= "redirect:../secure/admin/";
					break;
				case 2:
					redirectView = "redirect:../secure/branch_manager";
					break;
				case 3:
					redirectView = "redirect:../secure/agent";
					break;
				default:
					redirectView = "redirect:../";
					break;
				}

			}

		}
		return redirectView;
	}
   		 
}